<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\ShopSettings;
use Auth;

class ShopController extends BaseController
{
    /**
     * Create a new ProductController instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }


    public function index()
    {
        $userId = Auth::user()->id;
        $settings = ShopSettings::join('user_shop', 'user_shop.shop_id', '=', 'shop_settings.shop_id')
                    ->where('user_shop.user_id', $userId)
                    ->first();
        return view('admin.shop_base_settings')
                ->with('settings',$settings);
    }

    // /**
    //  * 店铺设置添加
    //  * @return [type] [description]
    //  */
    // public function create()
    // {
    //     // $settings = ShopSettings::where('shop_id', 1)->first();
    //     // return view('admin.shop_base_settings', ['settings'=>$settings]);
    // }

    /**
     * 店铺基本设置
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function store(Request $request)
    {
        $this->validate($request,[
            'shop_name' => 'required|max:20',
            'logo' => 'required|mimes:jpg,jpeg,bmp,png'
        ]);
        $logo = $request->file('logo');
        $logoPath = 'attachs/img/';
        $logoName = md5(time().$logo->getClientOriginalName().rand(1,999)).'.'.$logo->getClientOriginalExtension();
        $res = $logo->move($logoPath, $logoName);

        $userId = Auth::user()->id;
        $query = ShopSettings::join('user_shop', 'user_shop.shop_id', '=', 'shop_settings.shop_id')
                    ->where('user_shop.user_id', $userId)
                    ->first();
        $shopSetting = ShopSettings::where('shop_id', $query->shop_id)->first();
        $shopSetting->shop_name = $request->shop_name;
        $shopSetting->logo = $logoName;
        
        if ($shopSetting->save()) {
            return redirect('admin/shop')->with('succcess', '设置成功');
        }
        return redirect('admin/shop')->withErrors('设置失败');
 
    }

    public function edit($shopId) 
    {
        $settings = ShopSettings::where('shop_id', $shopId)->first();
        return view('admin.shop_base_settings_edit', ['settings'=>$settings]);
    }

    /**
     * 公司logo创建
     * @return [type] [description]
     */
    public function faceCreate()
    {
        return view('admin.shop_base_face');
    }

}
